// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema

const validator = {
  user_id: {
    rules: [
      {
        required: true,
      },
      {
        format: "string",
      },
    ],
    defaultValue: {
      $env: "uid",
    },
  },
  category_id: {
    rules: [
      { required: true },
      {
        format: "string",
      },
    ],
    title: "分类",
    label: "分类",
  },
  avatar: {
    rules: [
      {
        required: true,
      },
      {
        validateFunction: function (rule, value, data, callback) {
          // 处理对象格式的图片数据
          const url = typeof value === "object" ? value.url : value;
          if (!url) {
            callback("请上传活动简介图片");
            return;
          }
          // 验证图片URL格式
          const pattern = /^(http|https):\/\/.*\.(jpg|jpeg|png|gif|bmp|webp)$/;
          if (!pattern.test(url)) {
            callback("图片格式不正确");
            return;
          }
          callback();
        },
      },
    ],
    label: "活动简介",
    title: "活动简介",
  },
  content: {
    rules: [
      {
        required: true,
      },
      {
        validateFunction: function (rule, value, data, callback) {
          // 处理对象格式的图片数据
          const url = typeof value === "object" ? value.url : value;
          if (!url) {
            callback("请上传活动详情图片");
            return;
          }
          // 验证图片URL格式
          const pattern = /^(http|https):\/\/.*\.(jpg|jpeg|png|gif|bmp|webp)$/;
          if (!pattern.test(url)) {
            callback("图片格式不正确");
            return;
          }
          callback();
        },
      },
    ],
    label: "活动详情",
    title: "活动详情",
  },
  article_status: {
    rules: [
      {
        format: "int",
      },
      {
        range: [
          {
            value: 0,
            text: "草稿箱",
          },
          {
            value: 1,
            text: "已发布",
          },
          {
            value: 2,
            text: "已过期",
          },
        ],
      },
    ],
    title: "状态",
    defaultValue: 0,
    label: "状态",
  },
  view_count: {
    rules: [
      {
        format: "int",
      },
    ],
    title: "阅读数量",
    label: "阅读数量",
  },
  like_count: {
    rules: [
      {
        format: "int",
      },
    ],
  },
  last_comment_user_id: {
    rules: [
      {
        format: "string",
      },
    ],
  },
  publish_date: {
    rules: [
      {
        format: "timestamp",
      },
    ],
    title: "发表时间",
    defaultValue: {
      $env: "now",
    },
    label: "发表时间",
  },
  last_modify_date: {
    rules: [
      {
        format: "timestamp",
      },
    ],
    title: "最后修改时间",
    defaultValue: {
      $env: "now",
    },
    label: "最后修改时间",
  },
  title: {
    rules: [
      {
        required: true,
      },
      {
        format: "string",
      },
    ],
    label: "活动名称",
    title: "活动名称",
  },
  coupon_id: {
    rules: [
      {
        format: "string",
      },
    ],
    defaultValue: "",
  },
  start_time: {
    rules: [
      {
        required: true,
      },
      {
        format: "timestamp",
      },
    ],
    label: "开始时间",
    title: "开始时间",
  },
  end_time: {
    rules: [
      {
        required: true,
      },
      {
        format: "timestamp",
      },
    ],
    label: "结束时间",
    title: "结束时间",
  },
  location_name: {
    rules: [
      {
        required: true,
      },
      {
        format: "string",
      },
    ],
    label: "活动地点",
    title: "活动地点名称",
  },
  region_address: {
    rules: [
      {
        required: true,
      },
      {
        format: "object",
      },
      {
        validateFunction: function (rule, value, data, callback) {
          if (
            !value ||
            !value.text ||
            value.text.length !== 3 ||
            !value.value ||
            value.value.length !== 3
          ) {
            callback("请选择完整的省市区信息");
            return;
          }
          callback();
        },
      },
    ],
    title: "省市区信息",
    label: "省市区信息",
  },
};

const enumConverter = {
  article_status_valuetotext: {
    0: "草稿箱",
    1: "已发布",
    2: "已过期",
  },
};

function filterToWhere(filter, command) {
  let where = {};
  for (let field in filter) {
    let { type, value } = filter[field];
    switch (type) {
      case "search":
        if (typeof value === "string" && value.length) {
          where[field] = new RegExp(value);
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = [];
          for (let s of value) {
            selectValue.push(command.eq(s));
          }
          where[field] = command.or(selectValue);
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0];
          let lt = value[1];
          where[field] = command.and([command.gte(gt), command.lte(lt)]);
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value;
          let startDate = new Date(s);
          let endDate = new Date(e);
          where[field] = command.and([
            command.gte(startDate),
            command.lte(endDate),
          ]);
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value;
          where[field] = command.and([
            command.gte(startDate),
            command.lte(endDate),
          ]);
        }
        break;
    }
  }
  return where;
}

export { validator, enumConverter, filterToWhere };
